package nc.hw;

public class HJ29 {

    private static String encode(String word) {
        char[] chars = word.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char aChar : chars) {
            if (aChar >= 'A' && aChar <= 'Z') {
                sb.append((char) ((aChar - 'A' + 1) % 26 + 'a'));
            } else if (aChar >= 'a' && aChar <= 'z') {
                sb.append((char) ((aChar - 'a' + 1) % 26 + 'A'));
            } else if (aChar >= '0' && aChar <= '9') {
                sb.append((char) ((aChar - '0' - 1) % 10 + '0'));
            } else {
                sb.append(aChar);
            }
        }
        return sb.toString();
    }

    private static String decode(String word) {
        char[] chars = word.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char aChar : chars) {
            if (aChar >= 'A' && aChar <= 'Z') {
                sb.append((char) ((aChar - 'A' + 25) % 26 + 'a'));
            } else if (aChar >= 'a' && aChar <= 'z') {
                sb.append((char) ((aChar - 'a' + 25) % 26 + 'A'));
            } else if (aChar >= '0' && aChar <= '9') {
                sb.append((char) ((aChar - '0' + 9) % 10 + '0'));
            } else {
                sb.append(aChar);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(encode("abcdefg"));
        System.out.println(decode("BCDEFGH"));
    }

}
